import { Response } from 'express';
import { HttpStatusCode, ErrorCode } from '../types/statusCodes';
import { ApiResponse, PaginatedResponse, ErrorResponse } from '../types/response';

export class ResponseWrapper {
  /**
   * 成功响应
   */
  static success<T>(
    res: Response,
    data?: T,
    message: string = '操作成功',
    code: number = HttpStatusCode.OK
  ): Response {
    const response: ApiResponse<T> = {
      success: true,
      code,
      message,
      data,
      timestamp: new Date().toISOString(),
    };

    return res.status(code).json(response);
  }

  /**
   * 分页响应
   */
  static paginated<T>(
    res: Response,
    data: T,
    pagination: {
      page: number;
      limit: number;
      total: number;
      totalPages: number;
    },
    message: string = '操作成功',
    code: number = HttpStatusCode.OK
  ): Response {
    const response: PaginatedResponse<T> = {
      success: true,
      code,
      message,
      data,
      pagination,
      timestamp: new Date().toISOString(),
    };

    return res.status(code).json(response);
  }

  /**
   * 错误响应
   */
  static error(
    res: Response,
    message: string = '操作失败',
    code: number = HttpStatusCode.INTERNAL_SERVER_ERROR,
    errorCode?: ErrorCode,
    details?: any
  ): Response {
    const response: ErrorResponse = {
      success: false,
      code,
      message,
      timestamp: new Date().toISOString(),
    };

    if (errorCode || details) {
      response.error = {
        code: errorCode || ErrorCode.UNKNOWN_ERROR,
        details,
      };
    }

    return res.status(code).json(response);
  }


  /**
   * 创建成功响应
   */
  static created<T>(res: Response, data?: T, message: string = '创建成功'): Response {
    return this.success(res, data, message, HttpStatusCode.CREATED);
  }

  /**
   * 无内容响应
   */
  static noContent(res: Response, message: string = '无内容'): Response {
    return this.success(res, undefined, message, HttpStatusCode.NO_CONTENT);
  }

  /**
   * 错误请求响应
   */
  static badRequest(res: Response, message: string = '请求参数错误', details?: any): Response {
    return this.error(res, message, HttpStatusCode.BAD_REQUEST, ErrorCode.VALIDATION_ERROR, details);
  }

  /**
   * 未授权响应
   */
  static unauthorized(res: Response, message: string = '未授权访问'): Response {
    return this.error(res, message, HttpStatusCode.UNAUTHORIZED, ErrorCode.ACCESS_DENIED);
  }

  /**
   * 禁止访问响应
   */
  static forbidden(res: Response, message: string = '禁止访问'): Response {
    return this.error(res, message, HttpStatusCode.FORBIDDEN, ErrorCode.ACCESS_DENIED);
  }

  /**
   * 资源未找到响应
   */
  static notFound(res: Response, message: string = '资源未找到'): Response {
    return this.error(res, message, HttpStatusCode.NOT_FOUND, ErrorCode.RESOURCE_NOT_FOUND);
  }

  /**
   * 冲突响应
   */
  static conflict(res: Response, message: string = '资源冲突'): Response {
    return this.error(res, message, HttpStatusCode.CONFLICT, ErrorCode.USER_ALREADY_EXISTS);
  }

  /**
   * 服务器错误响应
   */
  static internalError(res: Response, message: string = '服务器内部错误'): Response {
    return this.error(res, message, HttpStatusCode.INTERNAL_SERVER_ERROR, ErrorCode.UNKNOWN_ERROR);
  }
}